/**
 * 
 */
package com.adaptadores;


import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.activities.Imagen_list_inicio;
import com.callbacks.InterfaceLugares;
import com.clases.Lugar;


/**
 * @author Chris
 * 
 */
public class LugaresThumbAdapter extends BaseAdapter implements OnScrollListener{

    private final int columnWidth;

    private final Context context;

    private final ArrayList<Imagen_list_inicio> rows;

	private boolean loading;

	private int previousTotal;
	
	private InterfaceLugares mantenedor;

    public LugaresThumbAdapter(Context context, int columnWidth,InterfaceLugares mantenedor) {

        super();
        this.context = context;
        this.columnWidth = columnWidth;
        rows = new ArrayList<Imagen_list_inicio>();
        this.mantenedor = mantenedor;
    }

    /**
     * @param context
     * @param textViewResourceId
     */

    public void add(Lugar object) {

        rows.add(new Imagen_list_inicio(LayoutInflater.from(context), object, this.columnWidth));

    }

    public void clear() {

        rows.clear();
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return rows.size();
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int position) {

        return rows.get(position);
    }

    /*
     * (non-Javadoc)
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int arg0) {

        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return rows.get(position).getView(convertView);
    }
    
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        if (loading) {
            // if trying to loading

            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        // si se cargaron los items necesarios
        if (!(loading)
                    && (totalItemCount - visibleItemCount) <= (firstVisibleItem)) {

            if (getCount() > 1) {
                mantenedor.getLugares(this.getCount());
            }
            //
            loading = true;
        }
    }

	@Override
	public void onScrollStateChanged(AbsListView arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

}
